#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pii pair<int,int>
#define pb push_back
#define vec vector<int>
#define y1 y_____hahahaha_____1

using namespace std;

typedef long long ll;

int read(){int x = 0,sgn = 1;char ch = getchar();for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;for(;isdigit(ch);ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48); return x * sgn;}

const int N = 1e6 + 10;
const int mod = 1e9 + 7;

int fac[N],pre[N],suf[N],ans,tot,n,K;

int upd(int x){return x + (x >> 31 & mod);}

ll qp(ll x,int t){ll res = 1;for(;t;t >>= 1,x = x * x % mod) if(t & 1) res = res * x % mod;return res;}

int main(){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in;	K = in;
	for(int i = (pre[0] = suf[K + 3] = 1);i <= K + 3;i++) pre[i] = upd((ll) pre[i - 1] * (n - i) % mod);
	for(int i = K + 2;i >= 0;i--) suf[i] = upd((ll) suf[i + 1] * (n - i) % mod);
	for(int i = (fac[0] = 1);i <= K + 2;i++) fac[i] = (ll) fac[i - 1] * i % mod;
	for(int i = 1;i <= K + 2;i++){
		tot = upd(tot + qp(i,K) - mod); int x = (ll) pre[i - 1] * suf[i + 1] % mod;
		int y = (ll) fac[i - 1] * fac[K + 2 - i] % mod * ((K + 2 - i) & 1 ? mod - 1 : 1) % mod;
		ans = upd(ans + (ll) tot * x % mod * qp(y,mod-2) % mod - mod);
	}
	printf("%d\n",ans);
	return 0;
}
